import { showModal, showMessage, toast } from "~/composables/util";
import { useRouter } from "vue-router";
import { useStore } from "vuex";
import { ref, reactive } from "vue";
import { putUpdateWebPassword,putUpdatePassword } from "~/api/user/index";


// 修改密码
export function useRepassword() {
    //修改密码

    const router = useRouter();
    const store = useStore();
    const fromDrawerRef = ref(null);

    const form = reactive({
        password: "",
        re_password: "",
    });
    const rules = {
        password: [
            { required: true, message: "新密码不能为空", trigger: "blur" },
            { min: 6, max: 16, message: "新密码长度为6~16个字符", trigger: "blur" },
            {
                pattern: "(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[;.!@#$%^&*?]).{6,16}",
                message: "新密码必须同时包含：大写字母、小写字母、数字和特殊字符;.!@#$%^&*?",
                trigger: "blur",
            },
        ],
        re_password: [
            { required: true, message: "确认密码不能为空", trigger: "blur" },
            { min: 5, max: 15, message: "确认密码长度为5~15个字符", trigger: "blur" },
            {
                pattern: "(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[;.!@#$%^&*?]).{6,16}",
                message: "确认密码必须同时包含：大写字母、小写字母、数字和特殊字符;.!@#$%^&*?",
                trigger: "blur",
            },
        ],
    };
    const formRef = ref(null);

    const onSubmit = () => {
        formRef.value.validate((valid) => {
            if (!valid) {
                return false;
            }
            fromDrawerRef.value.showLoading();
            putUpdatePassword(store.state.user.id, form)
                .then((res) => {
                    toast("密码修改成功，请重新登录");
                    // 清除token信息与user状态
                    store.dispatch("logout");
                    // 跳转回登录页
                    router.push("/login");
                })
                .finally(() => {
                    fromDrawerRef.value.hideLoading();
                });
        });
    };
    const openRePasswordForm = () => fromDrawerRef.value.open();
    return {
        fromDrawerRef,
        form,
        rules,
        formRef,
        onSubmit,
        openRePasswordForm,
    };
}


export function useWebRepassword() {
    //修改密码

    const router = useRouter();
    const store = useStore();
    const fromDrawerRef = ref(null);

    const form = reactive({
        password: "",
        re_password: "",
    });
    const rules = {
        password: [
            { required: true, message: "新密码不能为空", trigger: "blur" },
            { min: 6, max: 16, message: "新密码长度为6~16个字符", trigger: "blur" },
            {
                pattern: "(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[;.!@#$%^&*?]).{6,16}",
                message: "新密码必须同时包含：大写字母、小写字母、数字和特殊字符;.!@#$%^&*?",
                trigger: "blur",
            },
        ],
        re_password: [
            { required: true, message: "确认密码不能为空", trigger: "blur" },
            { min: 5, max: 15, message: "确认密码长度为5~15个字符", trigger: "blur" },
            {
                pattern: "(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[;.!@#$%^&*?]).{6,16}",
                message: "确认密码必须同时包含：大写字母、小写字母、数字和特殊字符;.!@#$%^&*?",
                trigger: "blur",
            },
        ],
    };
    const formRef = ref(null);

    const onSubmit = () => {
        formRef.value.validate((valid) => {
            if (!valid) {
                return false;
            }
            fromDrawerRef.value.showLoading();
            putUpdateWebPassword(store.state.user.id, form)
                .then((res) => {
                    toast("密码修改成功，请重新登录");
                    // 清除token信息与user状态
                    store.dispatch("logout");
                    // 跳转回登录页
                    router.push("/login");
                })
                .finally(() => {
                    fromDrawerRef.value.hideLoading();
                });
        });
    };
    const openWebRePasswordForm = () => fromDrawerRef.value.open();
    return {
        fromDrawerRef,
        form,
        rules,
        formRef,
        onSubmit,
        openWebRePasswordForm,
    };
}

// 删除cookie
export function deleteCookie(name) {
    document.cookie = `${name}=; expires=${new Date(0)}; path=/;`;
  }

// 退出登录
export function useLogout() {
    const router = useRouter();
    const store = useStore();

    function handleLogout() {
        showModal("是否要退出登录？")
            .then((res) => {
                // 清除token信息与user状态
                store.dispatch("logout");
                // 跳转回登录页
                router.push("/login");
                
                // 提示退出成功
                showMessage("退出成功！", "success");
            })
            .catch((error) => {
                console.log(error)
                showMessage("取消退出！", "info");
            });
    }
    return { handleLogout }
}